﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

public partial class demoPageRegisterSeries : System.Web.UI.Page
{
    private List<KeyValuePair<string, string>> KVPs = new List<KeyValuePair<string, string>>
    {
          new KeyValuePair<string, string>("RegisterArrayDeclaration", "觀察是否有新增Array")
         ,new KeyValuePair<string, string>("RegisterClientScriptBlock", "觀察是否有新增ScriptBlock")
         ,new KeyValuePair<string, string>("RegisterClientScriptInclude", "觀察是否有新增ScriptInclude")
         ,new KeyValuePair<string, string>("RegisterExpandoAttribute", "觀察是否有新增ExpandoAttribute")
         ,new KeyValuePair<string, string>("RegisterHiddenField", "觀察是否有新增HiddenField")
         ,new KeyValuePair<string, string>("RegisterOnSubmitStatement", "觀察是否有新增SubmitStatement")
         ,new KeyValuePair<string, string>("RegisterStartupScript", "觀察是否有新增StartupScript")
    };

    #region PutMessage
    private void PutMessage()
    {
        MethodBase caller = (new StackTrace()).GetFrame(1).GetMethod();
        string funcName = caller.Name;

        string Key = funcName.Replace("demo", "");

        KeyValuePair<string, string> KVP = KVPs.Find((KeyValuePair<string, string> item) =>
        {
            return item.Key == Key;
        });

        string message = KVP.Value;

        form1.Controls.Add(new Label { Text = message });
    }
    #endregion

    #region initailize
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            initDropDownList();
    }

    private void initDropDownList()
    {
        ddl.Items.Clear();

        foreach (KeyValuePair<string, string> KVP in KVPs)
            ddl.Items.Add(new ListItem(KVP.Key, KVP.Key));
    }
    #endregion

    #region demoRegisterArrayDeclaration
    public void demoRegisterArrayDeclaration()
    {
        string arrayName = "aArray";
        string arrayValue = "'1','2','3'";
        Page.ClientScript.RegisterArrayDeclaration(arrayName, arrayValue);

        PutMessage();
    }
    #endregion
    #region demoRegisterClientScriptBlock
    public void demoRegisterClientScriptBlock()
    {
        string key = "ScriptBlock";
        
        if (!Page.ClientScript.IsClientScriptBlockRegistered(key))
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), key, getScriptBlock());
        }

        PutMessage();
    }
    
    private string getScriptBlock()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<script type=\"text/javascript\">");
        sb.Append("function ScriptBlock(){alert('I am a ScriptBlock function');}");
        sb.Append("</script>");
        return sb.ToString();
    }
    #endregion
    #region demoRegisterClientScriptInclude
    public void demoRegisterClientScriptInclude()
    {
        string key = "ScriptInclude";
        string url = "http://code.jquery.com/jquery-1.6.3.min.js";
        
        if (!Page.ClientScript.IsClientScriptIncludeRegistered(key))
        {
            Page.ClientScript.RegisterClientScriptInclude(key, url);
        }
        PutMessage();
    }
    #endregion
    #region demoRegisterExpandoAttribute
    public void demoRegisterExpandoAttribute()
    {
        form1.Controls.Add(new TextBox { ID = "TextBox1" });

        Page.ClientScript.RegisterExpandoAttribute("TextBox1", "AnExpandoAttribute", "this is ExpandoAttribute");

        PutMessage();
    }
    #endregion
    #region demoRegisterHiddenField
    public void demoRegisterHiddenField()
    {
        string hiddenFieldName = "A_HiddenField";
        string hiddenFieldInitialValue = "this is a hidden value";

        Page.ClientScript.RegisterHiddenField(hiddenFieldName, hiddenFieldInitialValue);

        PutMessage();
    }
    #endregion
    #region demoRegisterOnSubmitStatement
    public void demoRegisterOnSubmitStatement()
    {
        form1.Controls.Add(new Button { ID = "Button1" });

        string key = "SubmitStatement";

        if (!Page.ClientScript.IsOnSubmitStatementRegistered(key))
        {
            Page.ClientScript.RegisterOnSubmitStatement(typeof(Button), key, getSubmitStatement());
        }

        PutMessage();
    }

    private string getSubmitStatement()
    {
        string script = "alert('this is a submit statement');";

        return script;
    }
    #endregion
    #region demoRegisterStartupScript
    public void demoRegisterStartupScript()
    {
        string key = "ScriptBlock";

        if (!Page.ClientScript.IsClientScriptBlockRegistered(key))
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), key, getScriptBlock());
        }

        key = "StartupScript";

        if (!Page.ClientScript.IsStartupScriptRegistered(key))
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), key, getStartupScript());
        }

        PutMessage();
    }

    private string getStartupScript()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<script type=\"text/javascript\">");
        sb.Append("ScriptBlock();");
        sb.Append("</script>");
        return sb.ToString();
    }
    #endregion

    #region ddl_SelectedIndexChanged
    protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;

        string value = ddl.SelectedValue;

        string methodName = "demo" + value;

        Call(methodName);
    }

    protected void Call(string methodName)
    {
        demoPageRegisterSeries instance = this;
        MethodInfo mi = instance.GetType().GetMethod(methodName);
        mi.Invoke(instance, null);
    }
    #endregion


} // end of demoPageRegisterSeries
